package com.zonta.finance.serivce.impl;


import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.common.exception.BizException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.UpdatePasswordForm;
import com.zonta.finance.domain.po.MemberBindPhone;
import com.zonta.finance.mapper.MemberBindPhoneMapper;
import com.zonta.finance.serivce.IMemberBindPhoneService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表绑定手机表 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-20
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MemberBindPhoneServiceImpl extends ServiceImpl<MemberBindPhoneMapper, MemberBindPhone> implements IMemberBindPhoneService {
    private final PasswordEncoder passwordEncoder;
    private final TokenService<AdminDTO> tokenService;
    @Override
    public void register(String phone, Long memberId, String password) {
        MemberBindPhone memberBindPhone = new MemberBindPhone();
        memberBindPhone.setMemberId(memberId);
        memberBindPhone.setPhone(phone);
        memberBindPhone.setPassword(passwordEncoder.encode(password));
        memberBindPhone.initDefault();
        if (!save(memberBindPhone)) {
            throw new BizException("会员绑定手机失败！");
        }
    }

    /**
     * 修改密码
     * @param form
     * @return
     */
    @Override
    public Boolean updatePassword(UpdatePasswordForm form) {
        if (form.getPassword().equals(form.getConfirmPassword())) {
            throw new BizException("两次输入密码不一样");
        }
        MemberBindPhone memberBindPhone = getById(tokenService.getThreadLocalUserId());
        if (ObjUtil.isEmpty(memberBindPhone)) {
            throw new BizException("账号信息不存在");
        }
        if (!passwordEncoder.matches(form.getOldPassword(),memberBindPhone.getPassword())) {
            throw new BizException("旧密码不正确");
        }
        String newPassword = passwordEncoder.encode(form.getPassword());
        LambdaUpdateWrapper<MemberBindPhone> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(MemberBindPhone::getPassword,newPassword)
                .eq(MemberBindPhone::getMemberId,tokenService.getThreadLocalUserId());
        boolean flag = update(wrapper);
        if (flag) {
            throw new BizException("密码修改失败");
        }
        return flag;
    }

    /**
     * 修改手机号（不可直接调用
     * @param phone
     * @return
     */
    @Override
    public Boolean updatePhone(String phone) {
        LambdaUpdateWrapper<MemberBindPhone> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(MemberBindPhone::getPhone,phone)
                .eq(MemberBindPhone::getMemberId,tokenService.getThreadLocalUserId());
        return update(wrapper);
    }

    @Override
    public MemberBindPhone getMemberByPhone(String phone) {
        LambdaQueryWrapper<MemberBindPhone> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberBindPhone::getPhone,phone);
        return getOne(wrapper);
    }
}
